using System;
using System.Collections.Generic;
using System.Text;

namespace codegen.template
{
    public class TObj 
    {

        #region TGeneric Members

        public static void CodeGenerate(string className, string[] fieldList)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<?php" + Environment.NewLine);
            sb.AppendFormat("class {0} implements Serializable" + Environment.NewLine,className);
            sb.Append("{" + Environment.NewLine);

            //property
            foreach (string sField in fieldList)
            {
                sb.AppendFormat("        public ${0};" + Environment.NewLine, sField);
            }
            sb.Append("" + Environment.NewLine);

            //construct
            sb.AppendFormat("        function {0}()" + Environment.NewLine,className);
            sb.Append("        {" + Environment.NewLine);
            sb.Append("                " + Environment.NewLine);
            sb.Append("        }" + Environment.NewLine);

            //serialize/deserialize
            sb.Append("        public function serialize()" + Environment.NewLine);
            sb.Append("        {" + Environment.NewLine);
            sb.Append("                return wddx_serialize_value($this);" + Environment.NewLine);
            sb.Append("        }" + Environment.NewLine);
            sb.Append("        public function unserialize($data)" + Environment.NewLine);
            sb.Append("        {" + Environment.NewLine);
            sb.Append("                $oo = wddx_deserialize($data);" + Environment.NewLine);

            foreach (string sField in fieldList)
            {
                sb.AppendFormat("                $this->{0} = $oo->{0};" + Environment.NewLine,sField);
            }
            sb.Append("        }" + Environment.NewLine);

            sb.Append("}" + Environment.NewLine);

            sb.Append("?>");

            string text = sb.ToString();

            utility.TextFile.Write(string.Format("class.{0}.php",className),text);

        }
        #endregion
    }
}
